home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / ICAppSourceKit1.0 / ICIconSuites.p < prev    next >
Encoding:
Text File  |  1994-10-31  |  5.4 KB  |  220 lines  |  [TEXT/PJMM]

  1. unit ICIconSuites;
  2.  
  3. interface
  4.  
  5.     procedure InitICIconSuites;
  6.     function GetDTDBIconSuiteCached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  7.  
  8. implementation
  9.  
  10.     uses
  11.         ICGlobals, ICMiscSubs, IconFamilies;
  12.  
  13.     function GetDTDBIcon (ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; var pbdt: DTPBRec): OSErr;
  14.         function GetTheIcon (dtrn: integer): boolean;
  15.         begin
  16.             pbdt.ioDTRefNum := dtrn;
  17.             pbdt.ioTagInfo := 0;
  18.             pbdt.ioIconType := icon_to_get;
  19.             pbdt.ioFileCreator := fcreator;
  20.             pbdt.ioFileType := ftype;
  21.             GetTheIcon := PBDTGetIconSync(@pbdt) = noErr;
  22.         end;
  23.  
  24.         var
  25.             oe: OSErr;
  26.             i: integer;
  27.             found: boolean;
  28.             junkstr: Str63;
  29.             vrefnum: OSErr;
  30.             crdate: longint;
  31.     begin
  32.         found := false;
  33.         if system7 then begin
  34.             if cookie = 0 then begin
  35.                 i := 1;
  36.                 repeat
  37.                     vrefnum := 0;
  38.                     junkstr := '';
  39.                     oe := GetVolInfo(junkstr, vrefnum, i, crdate);
  40.                     i := i + 1;
  41.                     if oe = noErr then begin
  42.                         with pbdt do begin
  43.                             ioNamePtr := nil;
  44.                             ioVRefNum := vrefnum;
  45.                             oe := PBDTGetPath(@pbdt);
  46.                             if oe = noErr then begin
  47.                                 if GetTheIcon(pbdt.ioDTRefNum) then begin
  48.                                     cookie := pbdt.ioDTRefNum;
  49.                                     found := true;
  50.                                 end;
  51.                             end;
  52.                         end;
  53.                         oe := noErr;
  54.                     end;
  55.                 until found or (oe <> noErr);
  56.             end
  57.             else begin
  58.                 found := GetTheIcon(cookie);
  59.             end;
  60.         end;
  61.         if found then begin
  62.             oe := noErr;
  63.         end
  64.         else begin
  65.             oe := afpItemNotFound;
  66.         end;
  67.         GetDTDBIcon := oe;
  68.     end; (* GetDTDBIcon *)
  69.  
  70.     var
  71.         icon_buffer: packed array[0..1023] of byte;
  72.  
  73.     function GetDTDBIconH (ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; var iconh: Handle): OSErr;
  74.         var
  75.             dtpb: DTPBRec;
  76.             err: OSErr;
  77.     begin
  78.         iconh := nil;
  79.         dtpb.ioDTBuffer := @icon_buffer;
  80.         dtpb.ioDTReqCount := sizeof(icon_buffer);
  81.         err := GetDTDBIcon(ftype, fcreator, cookie, icon_to_get, dtpb);
  82.         if err = noErr then begin
  83.             err := PtrToHand(@icon_buffer, iconh, dtpb.ioDTActCount);
  84.         end; (* if *)
  85.         if err <> noErr then begin
  86.             DisposeHandle(iconh);
  87.             iconh := nil;
  88.         end; (* if *)
  89.         GetDTDBIconH := err;
  90.     end; (* GetDTDBIconH *)
  91.  
  92.     function GetDTDBAddSuite (suite: Handle; ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; icon_to_put: OSType): OSErr;
  93.         var
  94.             err: OSErr;
  95.             iconh: Handle;
  96.     begin
  97.         err := GetDTDBIconH(ftype, fcreator, cookie, icon_to_get, iconh);
  98.         if err = noErr then begin
  99.             err := AddIconToSuite(iconh, suite, icon_to_put);
  100.         end; (* if *)
  101.         if err <> noErr then begin
  102.             DisposeHandle(iconh);
  103.             iconh := nil;
  104.         end; (* if *)
  105.         GetDTDBAddSuite := err;
  106.     end; (* GetDTDBAddSuite *)
  107.  
  108.     function GetDTDBIconSuiteUncached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  109.         var
  110.             err: OSErr;
  111.             junk: OSErr;
  112.             cookie: integer;
  113.     begin
  114.         suite := nil;
  115.         err := NewIconSuite(suite);
  116.         if err = noErr then begin
  117.             cookie := 0;
  118.             if GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLargeIcon, large1BitMask) = noErr then begin
  119.                 junk := GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLarge4BitIcon, large4BitData);
  120.                 junk := GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLarge8BitIcon, large8BitData);
  121.             end
  122.             else begin
  123.                 err := afpItemNotFound;
  124.             end; (* if *)
  125.         end; (* if *)
  126.         if err <> noErr then begin
  127.             if suite <> nil then begin
  128.                 junk := DisposeIconSuite(suite, true);
  129.                 suite := nil;
  130.             end; (* if *)
  131.         end; (* if *)
  132.         GetDTDBIconSuiteUncached := err;
  133.     end; (* GetDTDBIconSuiteUncached *)
  134.  
  135.     const
  136.         cache_max = 20;
  137.  
  138.     type
  139.         CacheRecord = record
  140.                 usage: longInt;
  141.                 ftype, fcreator: OSType;
  142.                 suite: handle;
  143.             end;
  144.  
  145.     var
  146.         cache: array[1..cache_max] of CacheRecord;
  147.         usage: longInt;
  148.         default_application_suite: handle;
  149.         default_document_suite: handle;
  150.  
  151.     function GetDTDBIconSuiteCached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  152.         var
  153.             err, junk: OSErr;
  154.             i, j: integer;
  155.             m: longInt;
  156.     begin
  157.         err := -1;
  158.         suite := nil;
  159.         for i := 1 to cache_max do begin
  160.             if (cache[i].usage > 0) & (cache[i].ftype = ftype) & (cache[i].fcreator = fcreator) then begin
  161.                 suite := cache[i].suite;
  162.                 err := noErr;
  163.                 cache[i].usage := usage;
  164.                 usage := usage + 1;
  165.                 leave;
  166.             end;
  167.         end;
  168.         if err <> noErr then begin
  169.             m := maxLongInt;
  170.             for i := 1 to cache_max do begin
  171.                 if (cache[i].usage < m) then begin
  172.                     j := i;
  173.                     m := cache[i].usage;
  174.                 end;
  175.             end;
  176.             err := GetDTDBIconSuiteUncached(ftype, fcreator, suite);
  177.             if err = noErr then begin
  178.                 if m > 0 then begin
  179.                     junk := DisposeIconSuite(cache[j].suite, true);
  180.                 end;
  181.                 cache[j].suite := suite;
  182.                 cache[j].ftype := ftype;
  183.                 cache[j].fcreator := fcreator;
  184.                 cache[j].usage := usage;
  185.                 usage := usage + 1;
  186.             end;
  187.         end;
  188.         if (err = noErr) & (suite = nil) then begin
  189.             err := resNotFound;
  190.         end;
  191.         if (err <> noErr) then begin
  192.             suite := default_document_suite;
  193.             if (ftype = 'APPL') & (default_application_suite <> nil) then begin
  194.                 suite := default_application_suite;
  195.             end;
  196.         end;
  197.         GetDTDBIconSuiteCached := err;
  198.     end;
  199.  
  200.     procedure InitICIconSuites;
  201.         var
  202.             i: integer;
  203.     begin
  204.         for i := 1 to cache_max do begin
  205.             cache[i].usage := -1;
  206.         end;
  207.         usage := 1;
  208.         default_application_suite := nil;
  209.         default_document_suite := nil;
  210.         if system7 then begin
  211.             if GetIconSuite(default_document_suite, -4000, svAllLargeData) <> noErr then begin
  212.                 default_document_suite := nil;
  213.             end; (* if *)
  214.             if GetIconSuite(default_application_suite, -3996, svAllLargeData) <> noErr then begin
  215.                 default_application_suite := nil;
  216.             end; (* if *)
  217.         end; (* if *)
  218.     end;
  219.  
  220. end.